package wwy.Service;

import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import wwy.Dao.CatalogueDao;
import wwy.Dto.CatalogueDto;
import wwy.Dto.ReadingDto;
import wwy.Entity.Book;
import wwy.Entity.Catalogue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ReadingService extends LogicService<CatalogueDao, Catalogue, Long>{
    private final HomeService homeService;
    private final BookDetailService bookDetailService;
    public ReadingService(@Autowired CatalogueDao catalogueDao, HomeService homeService, BookDetailService bookDetailService) {
        super(catalogueDao);
        this.homeService = homeService;
        this.bookDetailService = bookDetailService;
    }

    @Transactional
    public ReadingDto getReadingInfo(String bookName){
        ReadingDto readingDto = new ReadingDto();
        Book book = homeService.getBook(bookName);
        readingDto.setBookName(bookName);
        readingDto.setBookAddress(book.getBookAddress());
        readingDto.setTotalChar(book.getTotalChar());
        readingDto.setProgress(bookDetailService.getBookProgress(bookName));
        readingDto.setLocalBookAddress(book.getLocalBookAddress());
        return readingDto;
    }

    @Transactional
    public List<CatalogueDto> getBookCatalogues(String bookName){
        Book book = homeService.getBook(bookName);
        List<Catalogue> catalogues = dao.findCataloguesByBook(book);
        List<CatalogueDto> catalogueDTOS = new ArrayList<>();

        for (Catalogue catalogue : catalogues) {
            CatalogueDto catalogueDTO = new CatalogueDto();
            catalogueDTO.setChapterName(catalogue.getChapterName());
            catalogueDTO.setChapterPosition(catalogue.getChapterPosition());
            catalogueDTOS.add(catalogueDTO);
        }
        return catalogueDTOS;
    }

    @Transactional
    @EventListener(ApplicationReadyEvent.class)
    @Order(3)
    public void addDefaultReading(){
        if(dao.count() > 0) return;
        Book book = homeService.getBook("杀死一只知更鸟");
        List<Catalogue> catalogues = new ArrayList<>();
        List<Double> chapterPositions = Arrays.asList(5.0, 51.0, 11707.0, 18563.0, 27957.0,
                36961.0, 45635.0, 53595.0, 59419.0, 70265.0, 85113.0, 95065.0, 108210.0,
                108197.0, 119935.0, 127523.0, 137421.0, 148567.0, 158795.0, 171153.0,
                182785.0, 193157.0, 199257.0, 204655.0, 209849.0, 220509.0, 230557.0,
                234065.0, 239847.0, 245767.0, 258689.0, 262755.0, 268807.0);
        List<String> chapterNames = Arrays.asList("第一部", "第一章", "第二章", "第三章", "第四章", "第五章",
                "第六章", "第七章", "第八章", "第九章", "第十章", "第十一章", "第二部", "第十二章", "第十三章",
                "第十四章", "第十五章", "第十六章", "第十七章", "第十八章", "第十九章", "第二十章", "第二十一章",
                "第二十二章", "第二十三章", "第二十四章", "第二十五章", "第二十六章", "第二十七章", "第二十八章",
                "第二十九章", "第三十章", "第三十一章");
        for (int i = 0; i <= 32; i++){
            Catalogue catalogue = new Catalogue();
            catalogue.setChapterName(chapterNames.get(i));
            catalogue.setChapterPosition(chapterPositions.get(i));
            catalogue.setBook(book);
            catalogues.add(catalogue);
        }
        dao.saveAll(catalogues);
    }
}
